<template>
  <div>
    <a-tabs :activeKey="first_tab" @change="activeKeyChange">
      <a-tab-pane key="1" tab="未付">
        <a-card :bordered="false" :bodyStyle="tstyle">
          <div class="table-page-search-wrapper">
            <a-form layout="inline">
              <a-row :gutter="48">
                <a-col :md="6" :sm="24">
                  <a-form-item label="搜索條件">
                    <a-select  style="width: 100%" v-model="queryParam.searchType">
                      <a-select-option  style="width: 100%" :value="0">全部</a-select-option>
                      <a-select-option  style="width: 100%" :value="1">房產名</a-select-option>
                      <a-select-option  style="width: 100%" :value="2">房間名</a-select-option>
                      <a-select-option  style="width: 100%" :value="3">業主姓名</a-select-option>
                      <a-select-option  style="width: 100%" :value="4">業主手機號</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :md="8" :sm="24">
                  <a-form-item label="搜索值">
                    <a-input v-model="queryParam.searchValue" allow-clear placeholder="请输入搜索值"/>
                  </a-form-item>
                </a-col>
                <a-col :md="8" :sm="24">
                  <span class="table-page-search-submitButtons">
                    <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                    <a-button style="margin-left: 8px" @click="() => queryParam = { type: 1, numType: 0 }">重置</a-button>
                  </span>
                </a-col>
              </a-row>
            </a-form>
          </div>
        </a-card>
        <a-card :bordered="false">
          <div>
              <a-button @click="queryByNumType(0)" :class="queryParam.numType ===0 ? 'numTypeActive' : ''" type="dashed" size="small">全部{{ numTypeData.allNum }}</a-button>
              <a-button @click="queryByNumType(1)" :class="queryParam.numType ===1 ? 'numTypeActive' : ''" type="dashed" size="small">逾期{{ numTypeData.yqNum }}</a-button>
              <a-button @click="queryByNumType(2)" :class="queryParam.numType ===2 ? 'numTypeActive' : ''" type="dashed" size="small">今日交租{{ numTypeData.nowNum }}</a-button>
              <a-button @click="queryByNumType(3)" :class="queryParam.numType ===3 ? 'numTypeActive' : ''" type="dashed" size="small">清算賬單{{ numTypeData.qsNum }}</a-button>
          </div>
          <s-table
            ref="table"
            size="default"
            :columns="columns"
            :data="loadData"
            :extraTool="[]"
            :scroll="{ x: 1500 }"
            bordered
            :rowKey="(record) => record.id">
            <template slot="rentStartDate" slot-scope="text,record">
              {{ text }}~{{ record.rentEndDate }}
            </template>
            <template slot="rentAmountName" slot-scope="text,record">
            <a-row v-for="(item,index) in record.details" :key="index" class="aaa">
                <a-col :span="24" class="aa"><span>{{ item.rentAmountName }}</span></a-col>
              </a-row>
            </template>
            <template slot="amount" slot-scope="text,record">
              <a-row v-for="(item,index) in record.details" :key="index" class="aaa">
                <a-col :span="24" :class="item.amount < 0 ? 'abvv': 'ab'" class="aa"><span>{{ item.amount }}</span></a-col>
              </a-row>
            </template>
            <template slot="subtotal" slot-scope="text">
            <span  :class="text < 0 ? 'abvv': 'ab' ">{{ text }}</span>
            </template>
            <template slot="receiveAmount" slot-scope="text">
            <span  :class="text < 0 ? 'abvv': 'ab' ">{{ text }}</span>
            </template>
            <template slot="collectionType" slot-scope="text">
              <span>{{ statusFkfsFilter(text) }}</span>
            </template>
            <template slot="billStatus" slot-scope="text">
              <span v-if="text === 0">
                <a-tag color="orange">
                  {{ statusFilter(text) }}
                </a-tag>
              </span>
              <span v-else-if="text === 1">
                <a-tag color="green">
                  {{ statusFilter(text) }}
                </a-tag>
              </span>
              <span  v-else-if="text === 3">
                <a-tag color="purple">
                  {{ statusFilter(text) }}
                </a-tag>
              </span>
              <span  v-else>
                <a-tag color="cyan">
                  {{ statusFilter(text) }}
                </a-tag>
              </span>
            </template>
            <span slot="operation" slot-scope="text, record">
              <a @click="$refs.readOnlyForm.edit(record.id)">查看</a>
              <a-divider type="vertical" />
              <a-dropdown >
                  <a class="ant-dropdown-link">
                    更多 <a-icon type="down" />
                  </a>
                  <a-menu slot="overlay">
                    <a-menu-item v-if="record.billStatus ===1">
                      <a @click="$refs.arriveOwnerForm.edit(record.id, record.ownerLeaseId)">交租</a>
                    </a-menu-item>
                    <a-menu-item v-if="record.billStatus ===1">
                      <a @click="$refs.editOwnerFormOne.edit(record.id, record.remark, 1)">修改</a>
                    </a-menu-item>
                    <a-menu-item v-if="record.billStatus ===3">
                      <a @click="$refs.editTzqsOwnerFormOne.edit(record.id, record.remark, 2)">修改</a>
                    </a-menu-item>
                    <a-menu-item v-if="record.billStatus ===2">
                      <a-popconfirm  placement="topRight" title="撤銷到賬將刪除此賬單到賬時產生的流水？" @confirm="() => chexiaoDaoZhang(record.id)">
                        <a>撤銷交租</a>
                      </a-popconfirm>
                    </a-menu-item>
                    <a-menu-item v-if="record.billStatus ===3">
                      <a @click="$refs.arriveQsOwnerForm.edit(record.id, record.ownerLeaseId)">退租清算</a>
                    </a-menu-item>
                    <a-menu-item>
                      <a @click="$refs.editForm.edit(record.ownerLeaseId)">查看業主</a>
                    </a-menu-item>
                  </a-menu>
                </a-dropdown>
            </span>
          </s-table>
        </a-card>
      </a-tab-pane>
      <a-tab-pane key="2" tab="已付">
         <a-card :bordered="false" :bodyStyle="tstyle">
          <div class="table-page-search-wrapper">
            <a-form layout="inline">
              <a-row :gutter="48">
                 <a-col :md="4" :sm="24">
                  <a-form-item label="">
                     <a-checkbox v-model="queryParam2.searchNowType">
                     只看当前在租
                    </a-checkbox>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="搜索條件">
                    <a-select  style="width: 100%" v-model="queryParam2.searchType">
                      <a-select-option  style="width: 100%" :value="0">全部</a-select-option>
                      <a-select-option  style="width: 100%" :value="1">房產名</a-select-option>
                      <a-select-option  style="width: 100%" :value="2">房間名</a-select-option>
                      <a-select-option  style="width: 100%" :value="3">業主姓名</a-select-option>
                      <a-select-option  style="width: 100%" :value="4">業主手機號</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :md="8" :sm="24">
                  <a-form-item label="搜索值">
                    <a-input v-model="queryParam2.searchValue" allow-clear placeholder="请输入搜索值"/>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <span class="table-page-search-submitButtons">
                    <a-button type="primary" @click="$refs.table2.refresh(true)">查询</a-button>
                    <a-button style="margin-left: 8px" @click="() => queryParam2 = { type: 2, searchNowType: false }">重置</a-button>
                  </span>
                </a-col>
              </a-row>
            </a-form>
          </div>
        </a-card>
        <a-card :bordered="false">
          <s-table
            ref="table2"
            size="default"
            :columns="columns"
            :data="loadData2"
            :scroll="{ x: 1500}"
            :extraTool="[]"
            bordered
            :rowKey="(record) => record.id">
            <template slot="rentStartDate" slot-scope="text,record">
              {{ text }}~{{ record.rentEndDate }}
            </template>
            <template slot="rentAmountName" slot-scope="text,record">
            <a-row v-for="(item,index) in record.details" :key="index" class="aaa">
                <a-col :span="24" class="aa"><span>{{ item.rentAmountName }}</span></a-col>
              </a-row>
            </template>
            <template slot="amount" slot-scope="text,record">
              <a-row v-for="(item,index) in record.details" :key="index" class="aaa">
                <a-col :span="24" :class="item.amount < 0 ? 'abvv': 'ab'" class="aa"><span>{{ item.amount }}</span></a-col>
              </a-row>
            </template>
            <template slot="subtotal" slot-scope="text">
            <span  :class="text < 0 ? 'abvv': 'ab' ">{{ text }}</span>
            </template>
            <template slot="receiveAmount" slot-scope="text">
            <span  :class="text < 0 ? 'abvv': 'ab' ">{{ text }}</span>
            </template>
            <template slot="collectionType" slot-scope="text">
              <span>{{ statusFkfsFilter(text) }}</span>
            </template>
            <template slot="billStatus" slot-scope="text">
              <span v-if="text === 0">
                <a-tag color="orange">
                  {{ statusFilter(text) }}
                </a-tag>
              </span>
              <span v-else-if="text === 1">
                <a-tag color="green">
                  {{ statusFilter(text) }}
                </a-tag>
              </span>
              <span  v-else-if="text === 3">
                <a-tag color="purple">
                  {{ statusFilter(text) }}
                </a-tag>
              </span>
              <span  v-else>
                <a-tag color="cyan">
                  {{ statusFilter(text) }}
                </a-tag>
              </span>
            </template>
            <span slot="operation" slot-scope="text, record">
              <a @click="$refs.readOnlyForm.edit(record.id)">查看</a>
              <a-divider type="vertical" />
              <a-dropdown >
                  <a class="ant-dropdown-link">
                    更多 <a-icon type="down" />
                  </a>
                  <a-menu slot="overlay">
                    <a-menu-item v-if="record.billStatus ===2">
                      <a-popconfirm  placement="topRight" title="撤銷到賬將刪除此賬單到賬時產生的流水？" @confirm="() => chexiaoDaoZhang(record.id)">
                        <a>撤銷交租</a>
                      </a-popconfirm>
                    </a-menu-item>
                    <a-menu-item>
                      <a @click="$refs.editForm.edit(record.ownerLeaseId)">查看業主</a>
                    </a-menu-item>
                  </a-menu>
                </a-dropdown>
            </span>
          </s-table>
        </a-card>
      </a-tab-pane>
    </a-tabs>
    <read-only-form ref="readOnlyForm"/>
    <arrive-owner-form ref="arriveOwnerForm" @ok="handleOk" />
    <edit-owner-form-one ref="editOwnerFormOne" @ok="handleOk" />
    <edit-tzqs-owner-form-one ref="editTzqsOwnerFormOne" @ok="handleOk" />
    <arrive-qs-owner-form ref="arriveQsOwnerForm" @ok="handleOk" />
    <edit-form ref="editForm" @ok="handleOk"/>
  </div>
</template>
<script>
  import { STable } from '@/components'
  import { ownerLeaseBillPage, ownerLeaseBillCxdzEdit, getOwnerBillNum } from '@/api/modular/main/ownerleasebill/ownerLeaseBillManage'
  import { busMeansOfTransactionList } from '@/api/modular/main/busmeansoftransaction/busMeansOfTransactionManage'
  import readOnlyForm from './readOnlyForm.vue'
  import arriveOwnerForm from './arriveOwnerForm.vue'
  import editOwnerFormOne from './editOwnerFormOne.vue'
  import editTzqsOwnerFormOne from './editTzqsOwnerFormOne.vue'
  import arriveQsOwnerForm from './arriveQsOwnerForm.vue'
  import editForm from '../ownerlease/editForm.vue'
  export default {
    components: {
      STable,
      readOnlyForm,
      arriveOwnerForm,
      editOwnerFormOne,
      editTzqsOwnerFormOne,
      arriveQsOwnerForm,
      editForm
    },
    data () {
      return {
        first_tab: '1',
        // 查询参数
        queryParam: { type: 1, numType: 0 },
        queryParam2: { type: 2, searchNowType: false },
        // 表头
        // 表头
        columns: [
          // {
          //   title: '期數',
          //   align: 'center',
          //   dataIndex: 'qsNum',
          //   scopedSlots: { customRender: 'qsNum' }
          // },
           {
            title: '房間',
            align: 'center',
            dataIndex: 'roomName',
            key: 'roomName',
            fixed: 'left',
            width: 150
          },
          {
            title: '業主',
            align: 'center',
            dataIndex: 'name',
            key: 'name',
            fixed: 'left',
            width: 100
          },
          {
            title: '業主電話',
            align: 'center',
            dataIndex: 'phone',
            key: '1',
            width: 150
          },
          {
            title: '交租日',
            align: 'center',
            dataIndex: 'rentedDay',
            key: '2',
            width: 150
          },
          {
            title: '賬單週期',
            align: 'center',
            dataIndex: 'rentStartDate',
            scopedSlots: { customRender: 'rentStartDate' },
            key: '3',
            width: 150
          },
          {
            title: '類別',
            align: 'center',
            dataIndex: 'rentAmountName',
            scopedSlots: { customRender: 'rentAmountName' },
            key: '4',
            width: 100
          },
          {
            title: '金额',
            align: 'center',
            dataIndex: 'amount',
            scopedSlots: { customRender: 'amount' },
            key: '5',
            width: 150
          },
          {
            title: '小計',
            align: 'center',
            dataIndex: 'subtotal',
            scopedSlots: { customRender: 'subtotal' },
            key: '6',
            width: 150
          },
          {
            title: '實收金額',
            align: 'center',
            dataIndex: 'receiveAmount',
            scopedSlots: { customRender: 'receiveAmount' },
            key: '7',
            width: 150
          },
          {
            title: '實收日期',
            align: 'center',
            dataIndex: 'receiveDate',
            key: '8',
            width: 150
          },
          {
            title: '收款方式',
            align: 'center',
            dataIndex: 'collectionType',
            scopedSlots: { customRender: 'collectionType' },
            key: '9',
            width: 150
          },
          {
            title: '備註',
            align: 'center',
            dataIndex: 'remark',
            key: '10',
            width: 150
          },
          {
            title: '到账状态',
            align: 'center',
            dataIndex: 'billStatus',
            scopedSlots: { customRender: 'billStatus' },
            key: '11',
            width: 150
          },
          {
            title: '操作',
            key: 'action',
            fixed: 'right',
            width: 160,
            scopedSlots: { customRender: 'operation' }
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return ownerLeaseBillPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        loadData2: parameter => {
          return ownerLeaseBillPage(Object.assign(parameter, this.queryParam2)).then((res) => {
            return res.data
          })
        },
        numTypeData: {},
        collectionTypeData: [],
        billStatusDictTypeDropDown: [ { code: 1, value: '未交租' }, { code: 2, value: '已交租' }, { code: 3, value: '清算狀態' }, { code: 4, value: '清算到賬' } ]
      }
    },
    created () {
      this.initNeedData()
    },
    methods: {
       // 第一步tab点击
      activeKeyChange(key) {
        this.first_tab = key
      },
      initNeedData() {
        getOwnerBillNum().then((res) => {
          if (res.code === 200) {
            this.numTypeData = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
        busMeansOfTransactionList().then((res) => {
          if (res.code === 200) {
            this.collectionTypeData = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
      },
      statusFkfsFilter (id) {
        // eslint-disable-next-line eqeqeq
        const values = this.collectionTypeData.filter(item => item.id == id)
        if (values.length > 0) {
          return values[0].name
        }
      },
      statusFilter (sex) {
        // eslint-disable-next-line eqeqeq
        const values = this.billStatusDictTypeDropDown.filter(item => item.code == sex)
        if (values.length > 0) {
          return values[0].value
        }
      },
      chexiaoDaoZhang(id) {
        ownerLeaseBillCxdzEdit({ id: id }).then((res) => {
          if (res.code === 200) {
            this.handleOk()
          } else {
            this.$message.error(res.msg)//  + res.message
          }
        })
      },
      queryByNumType(type) {
        this.queryParam.numType = type
        this.$refs.table.refresh(true)
      },
      handleOk () {
        this.$refs.table.refresh()
        this.$refs.table2.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
  .aaa{
    border-bottom: 1px solid #ccc;
    margin-bottom: 6px;
  }
  .aa{
    text-align: center;
    font-size: 16px;
  }
  .ab{
    color: red;
  }
  .abvv{
    color: green;
  }
  .hhBum{
    margin-right: 10px;
    float: right;
  }
  .numTypeActive{
    color: #40a9ff;
    background-color: #fff;
    border-color: #40a9ff;
  }
</style>
